import sys
import settings
from PySide6.QtWidgets import QApplication, QWidget
from UIdata.event import Ui_Form_Mouse
from PySide6.QtCore import  QThread, Signal
from thread_work import mouse_work

# 鼠标监听对象
class MouseEvent(QWidget):
    start_mouse_listener = Signal(bool)

    """鼠标控件对象"""
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form_Mouse()
        self.ui.setupUi(self)

        self.info_dict = {
            'type':'鼠标',
            'button':None,
            'behavior':None,
            'count':None,
            'scroll':None,
            'start':None,
            'end':None,
            'key':None,
            'time':None,
            'key_string':None,
            'key_group':None,
        }

        self.connect_function()

    def connect_function(self):
        """信号连接"""
        self.ui.pushButton_4.clicked.connect(lambda :self.deleteLater())      # 删除按钮
        """信息字典更新信号"""
        self.ui.comboBox.currentTextChanged.connect(self.button_update)
        self.ui.comboBox_2.currentTextChanged.connect(self.behavior_update)
        self.ui.comboBox_3.currentTextChanged.connect(self.count_update)
        self.ui.pushButton.clicked.connect(lambda :self.but('start'))
        self.ui.pushButton_2.clicked.connect(lambda :self.but('end'))
        self.ui.pushButton_3.clicked.connect(lambda :self.but('scroll'))
        self.ui.spinBox.valueChanged.connect(self.time_update)

    """信息字典更新槽函数"""
    def button_update(self):
        self.info_dict['button'] = self.ui.comboBox.currentText()
        print(self.info_dict)
    def behavior_update(self):
        self.info_dict['behavior'] = self.ui.comboBox_2.currentText()
    def count_update(self):
        self.info_dict['count'] = self.ui.comboBox_3.currentText()
    def start_update(self,x_y):
        self.info_dict['start'] = x_y
        self.ui.pushButton.setText(str(x_y))
        print(f'位置更新{self.info_dict['start']}')
    def end_update(self,x_y):
        self.info_dict['end'] = x_y
        self.ui.pushButton_2.setText(str(x_y))
    def scroll_update(self,dx_dy):
        self.info_dict['scroll'] = dx_dy
        print(dx_dy)
        self.ui.pushButton_3.setText(str(dx_dy))
    def time_update(self,time):
        print(time)
        self.info_dict['time'] = time
    def scroll_update_warning(self):
        self.ui.pushButton_3.setText('不要点击鼠标')

    """监听按钮"""
    def but(self,info):
        """点击位置"""
        print('开始监听点击位置')
        self.start_mouse_listener.emit(True)

        self.thread = QThread()
        self.worker = mouse_work.Listener_Mouse_Position()
        self.worker.moveToThread(self.thread)
        self.thread.started.connect(self.worker.start)

        if info == 'start':
            self.worker.x_y.connect(self.start_update)
        elif info == 'end':
            self.worker.x_y.connect(self.end_update)
        elif info == 'scroll':
            self.worker.x_y.connect(self.scroll_update_warning)
            self.worker.dx_dy.connect(self.scroll_update)

        self.worker.finished.connect(self.a)
        self.worker.finished.connect(self.worker.deleteLater)
        self.worker.finished.connect(self.thread.quit)
        self.thread.finished.connect(self.thread.deleteLater)

        self.thread.start()

    def a(self):
        """显示主窗口"""
        self.start_mouse_listener.emit(False)





if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MouseEvent()
    window.show()
    sys.exit(app.exec())